class MaquinasController < ApplicationController
  # GET /maquinas
  # GET /maquinas.xml
    layout 'plantillaP'
  def index
    @maquinas = Maquina.find(:all, :conditions => ["cliente_id = ?", session[:id]])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @maquinas }
    end
  end

  # GET /maquinas/1
  # GET /maquinas/1.xml
  def mapa_maquinas
    @maquinas = Maquina.find(:all, :conditions => ["cliente_id = ?", session[:id]])
    respond_to do |format|
      format.html # show.html.erb
    end
  end
  def show
    @maquina = Maquina.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @maquina }
    end
  end

  # GET /maquinas/new
  # GET /maquinas/new.xml
  def new
    @maquina = Maquina.new
    @clientes = Cliente.all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @maquina }
    end
  end

  # GET /maquinas/1/edit
  def edit
    @maquina = Maquina.find(params[:id])
  end

  # POST /maquinas
  # POST /maquinas.xml
  def create

    @maquina = Maquina.new(params[:maquina])
    @maquina.cliente_id=session[:id]
    respond_to do |format|
      if @maquina.save
        logger.info 'LA maquina se creo exitosamente'
        flash[:notice] = 'Maquina was successfully created.'
        format.html { redirect_to(@maquina) }
        format.xml  { render :xml => @maquina, :status => :created, :location => @maquina }
      else
        @clientes = Cliente.all #hay que volver a llenar las variables necesarias pa pintar los combobox
        format.html { render :action => "new" }
        format.xml  { render :xml => @maquina.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /maquinas/1
  # PUT /maquinas/1.xml
  def update
    @maquina = Maquina.find(params[:id])

    respond_to do |format|
      if @maquina.update_attributes(params[:maquina])
        logger.info 'La maquina se edito exitosamente'
        flash[:notice] = 'Maquina was successfully updated.'
        format.html { redirect_to(@maquina) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @maquina.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /maquinas/1
  # DELETE /maquinas/1.xml
  def destroy
    @maquina = Maquina.find(params[:id])
    @maquina.destroy
    logger.info 'La maquina se elimino exitosamente'
    respond_to do |format|
      format.html { redirect_to(maquinas_url) }
      format.xml  { head :ok }
    end
  end
end
